# -*- coding: utf-8 -*-

import sys, os
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MainWindow(QDialog):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent=parent)

        self.setWindowTitle("Clipboard示例")

        textCopyBtn = QPushButton("&Copy Text")
        textPasteBtn = QPushButton("Paste &Text")
        htmlCopyBtn = QPushButton("C&opy HTML")
        htmlPasteBtn = QPushButton("Paste &HTML")
        imageCopyBtn = QPushButton("Co&py Image")
        imagePasteBtn = QPushButton("Paste &Image")

        self.textLabel = QLabel("Original Text")
        self.imageLabel = QLabel()
        self.imageLabel.setPixmap(QPixmap("./user_128px.png"))

        layout = QGridLayout()
        layout.addWidget(textCopyBtn, 0, 0)
        layout.addWidget(imageCopyBtn, 0, 1)
        layout.addWidget(htmlCopyBtn, 0, 2)
        layout.addWidget(textPasteBtn, 1, 0)
        layout.addWidget(imagePasteBtn, 1, 1)
        layout.addWidget(htmlPasteBtn, 1, 2)
        layout.addWidget(self.textLabel, 2, 0, 1, 2)
        layout.addWidget(self.imageLabel, 2, 2)
        self.setLayout(layout)

        textCopyBtn.clicked.connect(self.copy_text)
        textPasteBtn.clicked.connect(self.paste_text)
        imageCopyBtn.clicked.connect(self.copy_image)
        imagePasteBtn.clicked.connect(self.paste_image)
        htmlCopyBtn.clicked.connect(self.copy_html)
        htmlPasteBtn.clicked.connect(self.paste_html)

    def copy_text(self):
        clipboard = QApplication.clipboard()
        clipboard.setText("I`ve been clipped!")

    def paste_text(self):
        clipboard = QApplication.clipboard()
        self.textLabel.setText(clipboard.text())

    def copy_image(self):
        clipboard = QApplication.clipboard()
        clipboard.setPixmap(QPixmap("./002.jpg"))

    def paste_image(self):
        clipboard = QApplication.clipboard()
        self.imageLabel.setPixmap(clipboard.pixmap())

    def copy_html(self):
        mimeData = QMimeData()
        mimeData.setHtml("<b>Bold and <font color=red>Red</font></b>")
        clipboard = QApplication.clipboard()
        clipboard.setMimeData(mimeData)

    def paste_html(self):
        clipboard = QApplication.clipboard()
        mimeData = clipboard.mimeData()
        if mimeData.hasHtml():
            self.textLabel.setText(mimeData.html())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec())